/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import ControllerInterface.IControllerDataMap;
import ControllerInterface.IControllerMainMapping;
import ModelInterface.AbstractModelMapFactory;
import ModelInterface.IModelDataMap;
import ViewInterface.AbstractViewMapFactory;
import ViewInterface.IViewDataMap;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Fabian
 */
public class ControllerDataMap implements IControllerDataMap {

    private IControllerMainMapping myController;
    private IViewDataMap vistaDataMap;
    private IModelDataMap modeloDataMap;
    private DefaultTreeModel modeloDataset;
    private String separador;

    /**
     * 
     * @param datasetPath
     */
    @Override
    public void setDatasetPath(String datasetPath) {
        modeloDataMap.setConnection(datasetPath);

    }

    /**
     * 
     * @param myController
     */
    @Override
    public void setController(IControllerMainMapping myController) {
        this.myController = myController;
    }

    /**
     * 
     */
    @Override
    public void startDataMap() {
        vistaDataMap = AbstractViewMapFactory.getAbstractViewMapFactory().getVistaDataMap();
        vistaDataMap.setController(this);
        modeloDataMap = AbstractModelMapFactory.getAbstractModelMapFactory().getModelDataMap();

    }

    /**
     * 
     */
    @Override
    public void initDataMap() {
        String header = modeloDataMap.getHeader();
        createModel(header);
        myController.mapFromDataset(modeloDataMap, modeloDataset, separador);
    }

    /**
     * 
     * @param separador
     */
    @Override
    public void setSeparador(String separador) {
        this.separador = separador;
    }

    private void createModel(String header) {

        String[] columnas = header.split(separador);
        if (!header.contains(separador)) {
            return;
        }
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Dataset");
        DefaultMutableTreeNode table = new DefaultMutableTreeNode("Datos");

        for (int i = 0; i < columnas.length; i++) {
            DefaultMutableTreeNode column = new DefaultMutableTreeNode(columnas[i]+ " (data)");
            table.add(column);
        }
        root.add(table);
        modeloDataset = new DefaultTreeModel(root);
    }
}
